﻿using DevExpress.XtraEditors;
using MDK.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmFreezerOperation : BaseForm
    {
        private int position = 0;
        private int _Position { get { return position; } set { position = value; } }

        private Dictionary<int, int> posDic = new Dictionary<int, int>();
        public frmFreezerOperation()
        {
            InitializeComponent();
            InitTubeStatus();
            splashScreenManager1.ShowWaitForm();
            if (App.flow.BeginManaulHandle())
            {
                splashScreenManager1.CloseWaitForm();
            }
        }

        public void InitTubeStatus()
        {
            for (int i = 0; i < 6; i++)
            {
                SetPositionStatus(i);
            }
        }

        public void SetPositionStatus(int position)
        {
            posDic.Remove(position);
            var stack = App.flow.PosModel.FreezerPositions[position];
            if (stack.Count == 0)
            {
                SetNullPosition(position);
                posDic.Add(position, 1);
            }
            else
            {
                SetNormalPosition(position);
                posDic.Add(position, 2);
            }
        }

        public void SetNullPosition(int position)
        {
            var control = ((PictureEdit)(this.Controls.Find("pictureEdit" + (position + 1), true)[0]));
            control.Image = MDK.Winform.Properties.Resources.冷藏_空;
        }

        public void SetNormalPosition(int position)
        {
            var control = ((PictureEdit)(this.Controls.Find("pictureEdit" + (position + 1), true)[0]));
            control.Image = MDK.Winform.Properties.Resources.冷藏_非空;
        }

        private void 移除冷藏柜ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            App.flow.PosModel.FreezerPositions[_Position].Clear();
        }
 

        private void pictureEdit_Click(object sender, EventArgs e)
        {
            var controlname = (sender as PictureEdit).Name;
            int pos = 0;
            int.TryParse(controlname.Substring(11, controlname.Length - 11), out pos);
            position = pos - 1;
            contextMenuStrip.Show(Control.MousePosition);
        }

        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            移除冷藏柜ToolStripMenuItem.Visible = false;
            if (posDic[_Position] == 2)
            {
                移除冷藏柜ToolStripMenuItem.Visible = true;
            }
        }

        private void frmFreezerOperation_FormClosing(object sender, FormClosingEventArgs e)
        {
            App.flow.EndManaulHandle();
        }
    }
}
